package org.dreamwork.android.concurrent;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

/**
 * 广播接收器代理类. 主要是方便在 Activity/Fragment/Service 中使用.
 *
 * <p>使用方式，让 Activity/Fragment/Service 实现 {@link IBroadcastReceiverHandler} 接口，并代理给该类
 * <pre>
 *     public MyActivity extends Activity implements IBroadcastReceiverHandler {
 *         private BroadcastReceiver receiver;
 *
 *         &#64;Override
 *         public onResume () {
 *             super.onResumt ();
 *             
 *             if (receiver == null) {
 *                 receiver = new BroadcastReceiverDelegate (this);
 *                 ActionFilter filter = new ActionFilter ();
 *                 filter.addAction ("SomeAction");
 *                 registerReceiver (receiver, filter);
 *             }
 *         }
 *
 *         &#64;Override
 *         public onPause () {
 *             if (receiver != null) {
 *                 unregisterReceiver (receiver);
 *             }
 *             receiver = null;
 *
 *             super.onPause ();
 *         }
 *
 *         &#64;Override
 *         public void onReceive (Context context, Intent intent) {
 *             // 和普通的 BroadcastReceiver 一样实现
 *         }
 *
 *         ...
 *     }
 * </pre>
 * </p>
 * Created by game on 2016/9/7
 */
public class BroadcastReceiverDelegate extends BroadcastReceiver {
    private IBroadcastReceiverHandler handler;

    public BroadcastReceiverDelegate (IBroadcastReceiverHandler handler) {
        this.handler = handler;
    }

    @Override
    public void onReceive (Context context, Intent intent) {
        handler.onReceive (context, intent);
    }
}
